<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Role extends CI_Controller {

 function __construct()
 {
   parent::__construct();
   $this->load->model('muser');
   $this->load->library('form_validation');
   $this->load->library('pagination');
   $this->load->library('session');
 }
 function index(){ 	 		
 	 $roles=$this->session->userdata('roles');
	 $config['base_url'] = base_url().'role/index'; 
	 $config['total_rows'] = $this->muser->num_rows('Role');
	 
	 $config['per_page'] = 10; // xác định số record ở mỗi trang  
         $config['uri_segment'] = 3; // xác định segment chứa page number  
	 $this->pagination->initialize($config); 
	 $data['result']=$this->muser->getalldata('Role',$config['per_page'],$this->uri->segment(3));	
	 $role=array(
	 			0=>"0-admin",
	 			1=>"1-HR manager",
	 			2=>"2-HR executive",
	 );
	 $data['role']=$role;
	 if ($error=$this->session->userdata('error')) {
	 	$data['error']=$error;
		$this->session->unset_userdata('error'); 
	 }
         if($roles==0)
         {
	 $this->load->view('_header');
         $this->load->view('role/role_view',$data); 
         $this->load->view('_footer');
         }
         else
         {
             $data['ban_roles']="You haven't this role";
             $this->load->view('_header',$data);
             $this->load->view('notAllowRole');
             $this->load->view('_footer');
         }
 }
 function insert(){
 			$DT['email']=$this->input->post('email');	
		 	$DT['password']=md5('123456');			
			$DT['FullName']=$this->input->post('FullName');
			$DT['roles']=$this->input->post('roles');
			if ($this->check_Email($DT['email']))
			{
				$this->muser->addData('Role',$DT);				
			}else{
				$error['error']=$DT['email']. ' is existed in a Lab!';
				$this->session->set_userdata($error);
			}			
			redirect('role/index');
 }

 function delete($ID){
 	$this->muser->delColumn('Role','ID',$ID);
	redirect('role/index');
 }
 
function edit1 ()
 {
 	$DT['ID']=$this->input->post('ID');	
 	$DT['email']=$this->input->post('email');	
	$DT['FullName']=$this->input->post('FullName');
	$DT['roles']=$this->input->post('roles');
	$this->muser->updateData('Role','ID',$DT['ID'],$DT);	
	redirect('role/index');		
 } 
 function edit($str)
 {
 	$result=$this->muser->getRdata('Role','ID',$str);
	$data['result']=$result;
	 $role=array(
	 			0=>"0-admin",
	 			1=>"1-HR manager",
	 			2=>"2-HR executive",
	 );
	$data['role']=$role;	
 	$this->load->view('_header');
	$this->load->view('role/editRole_view',$data);	
	$this->load->view('_footer');
 }
 
 function check_Email($str){
 		return $this->muser->checkData('Role','email',$str);		
 }
}